<?php
/**
 * oscshop 电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015 oscshop.cn.
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
namespace Home\Controller;

class RedisController extends CommonController
{
    protected $push;

    protected function _initialize()
    {

    }

    public function test()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        $mywatchkey = $redis->get("mywatchkey");
        $rob_total = 1;   //抢购数量
        if($mywatchkey<$rob_total){
            $redis->watch("mywatchkey");
            $redis->multi();

            //设置延迟，方便测试效果。
           // sleep(5);
            //插入抢购数据
            $redis->hSet("mywatchlist","user_id_".mt_rand(1, 9999),time());
            $redis->set("mywatchkey",$mywatchkey+1);
            $rob_result = $redis->exec();
            if($rob_result){
                $mywatchlist = $redis->hGetAll("mywatchlist");
                echo "抢购成功 <br/>";
                echo "剩余数量：".($rob_total-$mywatchkey-1)."<br/>";
                echo "用户列表：<pre>";
                var_dump($mywatchlist);
            }else{
                echo "手气不好，再抢购！";exit;
            }
        }else{
            $mywatchlist = $redis->hGetAll("mywatchlist");
            var_dump($mywatchlist);

        }

        //S(array('type'=>'redis','expire'=>60));
        //$value=11111111111;
        //S('name',$value);
    }





}